//
//  TMTeacherDetailController.m
//  国学商家管理
//
//  Created by Tao Li on 2018/6/20.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "TMTeacherDetailController.h"
#import "TMTeacherDetailHeader.h"
#import "TMTeacherDescribeCell.h"
#import "TMTotalCommentCell.h"
#import "TMTeacherCommentCell.h"
#import "TMNearbyViewModel.h"
#import "TMShopCourseModel.h"
#import "TMCommentModel.h"
#import "WZNewspaperArticleImageViewController.h"
#import "TMCommityContentCell.h"
@interface TMTeacherDetailController ()
@property (strong, nonatomic) TMShopTeacherModel *models;
@property (nonatomic, strong) TMNearbyViewModel *viewModel;
@property (nonatomic, strong) NSMutableArray *listArray;
@property (nonatomic, strong) WZNewspaperArticleImageViewController *imageVC;
@property (nonatomic, assign) NSInteger pageNum;
@property (nonatomic, assign) NSInteger totalNum;
@property (nonatomic, strong) WZNoResultView *noResultView;
@end

@implementation TMTeacherDetailController
-(NSMutableArray *)listArray{
    if (!_listArray) {
        _listArray =[NSMutableArray array];
    }
    return _listArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.isTeacherDetail? @"教师详情":@"商家评价";
    [self registerCells];
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.viewModel = [[TMNearbyViewModel alloc]init];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //1 //2
        self.pageNum = 1;
        if (self.isTeacherDetail == YES) {
            [self.viewModel getTeacherDetailWithParam:self.tid AndPage:self.pageNum Success:^(id responseObject,id respon1) {
                self.models = responseObject;
                self.totalNum = self.models.totalPage;
                self.listArray = respon1;
                //[self noResultViewShowORNot];
                [self.tableView reloadData];
                [self.tableView.mj_header endRefreshing];
                [self.tableView.mj_footer endRefreshing];
            } Failed:^(NSString *error) {
                [self.tableView.mj_header endRefreshing];
                [self.tableView.mj_footer endRefreshing];
                //[self noResultViewShowORNot];
            }];
        }else{
            //self.tid
            [self.viewModel getShopCommentWithParam:self.tid AndPage:self.pageNum  Success:^(id responseObject,id total) {
                self.listArray = responseObject;
                self.totalNum = [total integerValue];
                [self noResultViewShowORNot];
                [self.tableView reloadData];
                [self.tableView.mj_header endRefreshing];
                [self.tableView.mj_footer endRefreshing];
            } Failed:^(NSString *error) {
                [self.tableView.mj_header endRefreshing];
                [self.tableView.mj_footer endRefreshing];
                [self noResultViewShowORNot];
            }];
        }
    }];
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.pageNum ++;
        if (self.pageNum > self.totalNum) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return;
        }
        if (self.isTeacherDetail == YES) {
            [self.viewModel getTeacherDetailWithParam:self.tid AndPage:self.pageNum Success:^(id responseObject,id respon1) {
                self.models = responseObject;
                self.listArray = respon1;
                [self.tableView reloadData];
                [self.tableView.mj_header endRefreshing];
                [self.tableView.mj_footer endRefreshing];
            } Failed:^(NSString *error) {
                if (self.pageNum > 1) {
                    self.pageNum--;
                }
                [self.tableView.mj_header endRefreshing];
                [self.tableView.mj_footer endRefreshing];
            }];
        }else{
            //self.tid
            [self.viewModel getShopCommentWithParam:self.tid AndPage:self.pageNum  Success:^(id responseObject,id total) {
                self.listArray = responseObject;
                [self.tableView reloadData];
                [self.tableView.mj_header endRefreshing];
                [self.tableView.mj_footer endRefreshing];
            } Failed:^(NSString *error) {
                if (self.pageNum > 1) {
                    self.pageNum--;
                }
                [self.tableView.mj_header endRefreshing];
                [self.tableView.mj_footer endRefreshing];
            }];
        }
    }];
    
    [self.tableView.mj_header beginRefreshing];
}
- (void)registerCells{
    [self.tableView registerNib:[UINib nibWithNibName:@"TMTeacherDescribeCell" bundle:nil] forCellReuseIdentifier:@"TMTeacherDescribeCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TMTeacherCommentCell" bundle:nil] forCellReuseIdentifier:@"TMTeacherCommentCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TMTotalCommentCell" bundle:nil] forCellReuseIdentifier:@"TMTotalCommentCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TMCommityContentCell" bundle:nil] forCellReuseIdentifier:@"TMCommityContentCell"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.isTeacherDetail == YES) {
        return 1;
    }else{
        return self.listArray.count;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.isTeacherDetail == YES) {
         return 2+self.listArray.count;
    }else{
        TMCommentModel *model = self.listArray[section];
        if (section == 0) {
            if (model.remark.length > 0) {
                return 3;
            }else{
                return 2;
            }
        }else{
        if (model.remark.length > 0) {
            return 2;
        }else{
            return 1;
        }
        }
    }

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TMCommityContentCell *replayCell = [tableView dequeueReusableCellWithIdentifier:@"TMCommityContentCell"];
    if (self.isTeacherDetail == YES) {
        if (indexPath.row == 0) {
            TMTeacherDescribeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TMTeacherDescribeCell"];
            cell.model = self.models;
            cell.block = ^(NSInteger index, NSMutableArray *list) {
                    self.imageVC = STORY_BOARD(@"Main", @"WZNewspaperArticleImageViewController");
                    self.imageVC.imageList = list;
                    self.imageVC.currentImageIndex = index-1;
                    [self.imageVC show];
                self.imageVC.view.frame = self.navigationController.view.bounds;
                    [self.navigationController pushViewController:self.imageVC animated:YES];
            };
            return cell;
        }
        else if (indexPath.row == 1) {
            TMTotalCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TMTotalCommentCell"];
            cell.totalNum.text = [NSString stringWithFormat:@"总体评价(%ld)", self.models.total];
            for (NSInteger i = 0; i < self.models.average; i ++) {
                UIButton *btn = cell.starcollection[i];
                btn.selected = YES;
            }
            return cell;
        }
        else{
            TMTeacherCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TMTeacherCommentCell"];
            cell.model = self.listArray[indexPath.row-2];
            
            return cell;
        }
    }else{
        
        if (indexPath.section == 0) {
            if (indexPath.row == 0) {
                TMTotalCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TMTotalCommentCell"];
                TMCommentModel *model = self.listArray[indexPath.section];
                cell.totalNum.text = [NSString stringWithFormat:@"总体评价(%ld)", model.total];
                for (NSInteger i = 0; i < model.average; i ++) {
                    UIButton *btn = cell.starcollection[i];
                    btn.selected = YES;
                }
                return cell;
            }
            if (indexPath.row == 1) {
                TMTeacherCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TMTeacherCommentCell"];
                //            TMCommentModel *model = self.listArray[indexPath.row - 1];
                cell.model = self.listArray[indexPath.section];
                return cell;
            }
            if (indexPath.row == 2) {
                TMCommentModel *model = self.listArray[indexPath.section];
                replayCell.contentLabel.text = [NSString stringWithFormat:@"商家回复:%@", model.remark];
                return replayCell;
            }
        }else{
            if (indexPath.row == 0) {
                TMTeacherCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TMTeacherCommentCell"];
                //            TMCommentModel *model = self.listArray[indexPath.row - 1];
                cell.model = self.listArray[indexPath.section];
                return cell;
            }
            if (indexPath.row == 1) {
                TMCommentModel *model = self.listArray[indexPath.section];
                replayCell.contentLabel.text = [NSString stringWithFormat:@"商家回复:%@", model.remark];
                return replayCell;
            }
        }
}
     return [UITableViewCell new];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (self.isTeacherDetail == YES) {
        TMTeacherDetailHeader *header = [[NSBundle mainBundle] loadNibNamed:@"TMTeacherDetailHeader" owner:self options:nil][0];
        header.techerModel = self.models;
        return header;
    }else{
        return nil;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (self.isTeacherDetail == YES) {
        return 202*kWindowW/375;
    }else{
        return 0;
    }

}
- (WZNoResultView *)noResultView{
    if (!_noResultView) {
        _noResultView = [[[NSBundle mainBundle] loadNibNamed:@"WZNoResultView" owner:nil options:nil]firstObject];
        if (self.isTeacherDetail == YES) {
            _noResultView.frame = CGRectMake(0, 202*kWindowW/375, kWindowW, kWindowH-202*kWindowW/375);
        }else {
            _noResultView.frame = CGRectMake(0, 0, kWindowW, kWindowH);
        }
        _noResultView.isSearchVC = YES;
        if (self.isTeacherDetail == NO) {
            [self.tableView addSubview:_noResultView];
        }
    }
    return _noResultView;
}
- (void)noResultViewShowORNot{
    if (!self.listArray.count) {
        self.noResultView.hidden = NO;
    }else{
        self.noResultView.hidden = YES;
    }
}
@end
